package com.yqg.manage.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.manage.common.interceptor.SysLog;
import com.yqg.manage.entity.Place;
import com.yqg.manage.mapper.ProblemNotificationMapper;
import com.yqg.manage.service.IPlaceService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.manage.common.Result;

import com.yqg.manage.service.IProblemNotificationService;
import com.yqg.manage.entity.ProblemNotification;

import org.springframework.web.bind.annotation.RestController;

/**
 * ProblemNotificationController 前端控制器
 *
 * @author zxf
 * @since 2025-08-06
 */
@RestController
@RequestMapping("/problem-notification")
public class ProblemNotificationController {

    @Resource
    private IProblemNotificationService problemNotificationService;

    @Resource
    private IPlaceService placeService;

    @Resource
    private ProblemNotificationMapper problemNotificationMapper;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    @SysLog("新增或者更新检查问题通知")
    public Result save(@RequestBody ProblemNotification problemNotification) {
        if (problemNotification.getId() == null) {
            problemNotification.setCreateTime(LocalDateTime.now());
        }
        LambdaQueryWrapper<Place> placeWrapper = new LambdaQueryWrapper<>();
        placeWrapper.eq(Place::getIsDelete, "0")
                .eq(Place::getCompanyLicense, problemNotification.getCompanyLicense());
        Place place = placeService.getOne(placeWrapper);
        if (place == null) {
            // 商户不存在，新增并绑定
            place = new Place();
            place.setCompanyName(problemNotification.getCompanyName());
            place.setCompanyLicense(problemNotification.getCompanyLicense());
            place.setEnterpriseType(problemNotification.getEnterpriseType());
            boolean b = placeService.saveOrUpdate(place);
            if (b) {
                problemNotification.setPlaceId(place.getId());
            } else {
                Result.error();
            }
        } else {
            // 商户存在，直接绑定
            problemNotification.setPlaceId(place.getId());
        }
        return Result.success(problemNotificationService.saveOrUpdate(problemNotification));
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    @SysLog("删除商户检查问题通知")
    public Result delete(@PathVariable Integer id) {
        ProblemNotification problemNotification = problemNotificationService.getById(id);
        problemNotification.setIsDelete("1");
        return Result.success(problemNotificationService.saveOrUpdate(problemNotification));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<ProblemNotification> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        List<ProblemNotification> list = problemNotificationService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(problemNotificationService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(required = false) String name,
                           @RequestParam(required = false) String status,
                           @RequestParam(required = false) String process) {
        pageNum = (pageNum - 1) * pageSize;
        Map<String,Object> map = new HashMap<>();
        map.put("records",problemNotificationMapper.getPageData(pageNum,pageSize,name,status,process));
        map.put("total",problemNotificationMapper.getPageDataCount(name,status,process));
        return Result.success(map);
    }

    /**
     * 通知状态修改
     */
    @PostMapping("/noticeUpdateStatus")
    @SysLog("下发商户问题通知")
    public  Result noticeUpdateStatus(@RequestBody ProblemNotification problemNotification){
        return Result.success(problemNotificationService.saveOrUpdate(problemNotification));
    }
}

